package com.satalyst.webtest.model.page;

import edu.umd.cs.findbugs.annotations.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static com.satalyst.util.Preconditions.nonEmpty;
import static com.satalyst.util.Preconditions.nonNull;

/**
 * @author Aidan Morgan
 */
public class Page {
    public static final String DEFAULT_PAGE_NAME = "Unnamed Page";
    private UUID id;
    private String name;
    private List<PageElement> elements;
    private List<Page> outgoingPages;
    private List<Page> incomingPages;

    public Page() {
        this(UUID.randomUUID(), DEFAULT_PAGE_NAME);
    }

    public Page(@NonNull UUID id, @NonNull String name) {
        this.id = nonNull(id);
        this.name = nonEmpty(name);
        this.elements = new ArrayList<PageElement>();
    }

    public UUID getId() {
        return id;
    }

    public void setId(@NonNull UUID id) {
        this.id = nonNull(id);
    }

    public String getName() {
        return name;
    }

    public void setName(@NonNull String name) {
        this.name = nonEmpty(name);
    }

    public List<PageElement> getElements() {
        return Collections.unmodifiableList(elements);
    }

    public void addPageElement(@NonNull PageElement element) {
        nonNull(element);
        if (!elements.contains(element)) {
            elements.add(element);
            element.setOwner(this);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Page page = (Page) o;

        return id.equals(page.id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return id.hashCode();
    }
}
